<template>
    <div>
        <h1 style="text-align: center;">角色列表</h1>
        <router-link to="/role/add">
            <el-button type="primary">添加</el-button>
        </router-link>
        <el-table
            :data="rolelist"
            style="width: 100%">
            <el-table-column
                prop="name"
                label="角色名称"
                width="180">
            </el-table-column>
            <el-table-column
                prop="auths"
                label="权限"
                width="180">
                <template slot-scope="scope">
                    <el-tree :data="scope.row.auths" :props="defaultProps" ></el-tree>
                </template>
            </el-table-column>
            <el-table-column
                label="操作">
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import {roleList, roleDel} from '@/request'
export default {
    data () {
        return {
            rolelist: [],
            defaultProps: {
                children: 'children',
                label: 'name'
            }
        }
    },
    methods: {
        getRoleList() {
            roleList().then(data => {
                console.log(data);
                this.rolelist = data.data.rolelist
            })
        },
        handleEdit(index, row) {
            // console.log(666, index);
            this.$router.push({path: '/role/edit', query: {id: row._id}})
        },
        handleDelete(index, row) {
            this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                // 发请求删除
                roleDel(row._id).then(data => {
                    let {msg, error_code} = data
                    if(error_code === 200) {
                        this.rolelist.splice(index, 1)
                        this.$message({
                            type: 'success',
                            message: msg
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
    },
    created() {
        this.getRoleList()
    },
}
</script>

<style>
    
</style>
